package org.scboy.logger.dubbo;

import java.util.Date;

import org.apache.dubbo.config.annotation.DubboService;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.logger.api.ILoggerManager;
import org.scboy.logger.dao.LoggerDbColumnLogDao;
import org.scboy.logger.dao.LoggerDbTableLogDao;
import org.scboy.logger.dao.LoggerRequestLogDao;
import org.scboy.utils.entitys.common.RequestEntity;
import org.scboy.utils.entitys.coobi.DataAlterColumn;
import org.scboy.utils.entitys.coobi.DataAlterTable;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;

@DubboService
public class LoggerManagerImpl implements ILoggerManager {	
			
	@Autowired
	EntityFactory facotry;
	
	@Override
	public void writeDataBaseLog(String exeType,DataAlterTable table) {
		JSONConfig config=new JSONConfig();
		config.setIgnoreNullValue(false);
		
		LoggerDbTableLogDao tableDao=facotry.getDao(LoggerDbTableLogDao.class);
		LoggerDbColumnLogDao columnDao=facotry.getDao(LoggerDbColumnLogDao.class);
		
		Date now=new Date();
		
		Model alterTable=Model.create();
		alterTable.add("id", UUID.randomUUID().toString());
		alterTable.add("token", table.getToken());
		alterTable.add("userId", table.getUserId());
		alterTable.add("userName", table.getUserName());
		alterTable.add("orgId", table.getOrgId());
		alterTable.add("requestId", table.getRequestId());
		alterTable.add("dbTable", table.getTableName());
		alterTable.add("exeType", exeType);
		alterTable.add("exeSql", table.getExeSql());
		alterTable.add("exeParams", JSONUtil.toJsonStr(table.getParams(),config));
		alterTable.add("tabComment", table.getComment());
		alterTable.add("logTime", now);
		alterTable.add("logTimeIndex", DateUtil.format(now, "yyyyMMdd"));
		
		StringBuilder primaryJoinKey=new StringBuilder("");
		//主键值
		for (DataAlterColumn column : table.getPrimary()) {
			if(primaryJoinKey.length()!=0) {
				primaryJoinKey.append("-");
			}
			primaryJoinKey.append(column.getPrimaryValue());
		}
		
		alterTable.add("primaryVerValue", MD5.create().digestHex(primaryJoinKey.toString()));
		
		tableDao.insert(alterTable);
		
		//变更列的值
		for (DataAlterColumn column : table.getColumns()) {
			Model alterColumn=Model.create();
			alterColumn.add("id", UUID.randomUUID().toString());
			alterColumn.add("token", table.getToken());
			alterColumn.add("userId", table.getUserId());
			alterColumn.add("userName", table.getUserName());
			alterColumn.add("orgId", table.getOrgId());
			alterColumn.add("requestId", table.getRequestId());
			alterColumn.add("exeType", exeType);
			alterColumn.add("tlId", alterTable.getString("id"));
			alterColumn.add("columnName", column.getColumnName());
			alterColumn.add("colComment", column.getComment());
			alterColumn.add("newValue", column.getNewValue());
			alterColumn.add("oldValue", column.getOldValue());
			alterColumn.add("newValueIndex", column.getNewValue().length()>50 ? column.getNewValue().substring(0,50):column.getNewValue());
			alterColumn.add("oldValueIndex", column.getOldValue().length()>50 ? column.getOldValue().substring(0,50):column.getOldValue());
			alterColumn.add("logTime", now);
			alterColumn.add("logTimeIndex", DateUtil.format(now, "yyyyMMdd"));
			columnDao.insert(alterColumn);
		}

	}

	@Override
	public void writeRequestLog(RequestEntity request) {
		JSONConfig config=new JSONConfig();
		config.setIgnoreNullValue(false);
		
		LoggerRequestLogDao requestDao=facotry.getDao(LoggerRequestLogDao.class);
		Date now=new Date();
		Model log=Model.create();
		log.add("id", request.getRequestId());
		log.add("token", request.getToken());
		log.add("userId", request.getUserId());
		log.add("userName", request.getUserName());
		log.add("orgId", request.getOrgId());
		log.add("name", request.getName());
		log.add("appName", request.getAppName());
		log.add("requestMethod", request.getMethod());
		log.add("url", request.getUrl());
		log.add("headData", JSONUtil.toJsonStr(request.getHeadData(),config));
		log.add("requestParams", JSONUtil.toJsonStr(request.getParams(),config));
		log.add("requestBody", request.getRequestBody());
		log.add("responseBody", request.getResponseBody());
		log.add("logTime", now);
		log.add("logTimeIndex", DateUtil.format(now, "yyyyMMdd"));
		requestDao.insert(log);
	}
}
